import { observable, action, configure, computed } from "mobx";
configure({ enforceActions: "always" });
import SyncStorage from "sync-storage";
import "../Utils/common";

const USER_TOKEN = "userToken";

export default class UserStore {
  @observable
  _avatarUrl: string =
    "https://s3.amazonaws.com/uifaces/faces/twitter/ladylexy/128.jpg";

  @observable
  _name: string = "Mike John";

  @observable
  _nickName: string = "Little Mike";

  @observable
  _phone: string = "18236484569";

  @observable
  _isNoticeReminder: boolean = true;

  @observable
  _fontSize: number = 14;

  @observable
  _company: string = "江苏运满满物流有限公司";

  @observable
  _isLogin: boolean = false;

  constructor() {
    this.syncStorageLoad();
  }

  @computed
  get avatarUrl() {
    return this._avatarUrl;
  }

  set avatarUrl(avatarUrl: string) {
    this._avatarUrl = avatarUrl;
  }

  @computed
  get name() {
    return this._name;
  }

  set name(name: string) {
    this._name = name;
  }

  @computed
  get company() {
    return this._company;
  }

  set company(company: string) {
    this._company = company;
  }

  @computed
  get isNoticeReminder() {
    return this._isNoticeReminder;
  }

  set isNoticeReminder(isNoticeReminder: boolean) {
    this._isNoticeReminder = isNoticeReminder;
  }

  @computed
  get fontSize() {
    return this._fontSize;
  }

  set fontSize(fontSize: number) {
    this._fontSize = fontSize;
  }

  @computed
  get nickName() {
    return this._nickName;
  }

  set nickName(nickName: string) {
    this._nickName = nickName;
  }

  @computed
  get phone() {
    return this._phone;
  }

  set phone(phone: string) {
    this._phone = phone;
  }

  // 先 getter 后 setter
  @computed
  get isLogin() {
    let userToken = this.getUserToken();
    if (userToken && userToken.length > 0) {
      return true;
    }
    return false;
  }

  set isLogin(isLogin: boolean) {
    if (isLogin === true) {
      this.setUserToken(String.random32());
    } else {
      this.setUserToken("");
    }
    this._isLogin = isLogin;
  }

  // other methods
  @action
  getUserToken = () => {
    let userToken = SyncStorage.get(USER_TOKEN);
    return userToken;
  };

  @action
  setUserToken = (userToken: string) => {
    SyncStorage.set(USER_TOKEN, userToken);
  };

  async syncStorageLoad() {
    await SyncStorage.init();
  }
}

const userStore = new UserStore();

export { userStore };
